package com.evan.review.lifecycle;

import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.evan.review.R;

/**
 * 配置变更演示Activity
 * 用于展示屏幕旋转等配置变更时Activity的生命周期
 * 包含两种模式：1. 默认重建模式 2. 配置变更处理模式
 */
public class ConfigChangeActivity extends AppCompatActivity {

    private static final String TAG = "ConfigChangeActivity";
    private TextView tvLifecycleInfo;
    private StringBuilder lifecycleLog = new StringBuilder();
    private int instanceCounter = 0;
    private static int totalInstances = 0;
    private String userInput = "";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config_change);
        
        // 记录实例编号
        instanceCounter = ++totalInstances;
        
        logLifecycleEvent("onCreate() - savedInstanceState: " + (savedInstanceState == null ? "null" : "not null") + " [实例#" + instanceCounter + "]");
        
        if (savedInstanceState != null) {
            // 从保存的状态恢复数据
            String savedLog = savedInstanceState.getString("lifecycle_log");
            userInput = savedInstanceState.getString("user_input", "");
            if (savedLog != null) {
                lifecycleLog = new StringBuilder(savedLog);
                logLifecycleEvent("Restored from savedInstanceState");
            }
        }
        
        initViews();
        updateLifecycleInfo();
    }
    
    private void initViews() {
        tvLifecycleInfo = findViewById(R.id.tv_lifecycle_info);
        
        TextView tvInstanceInfo = findViewById(R.id.tv_instance_info);
        tvInstanceInfo.setText("当前实例: #" + instanceCounter + " / 总实例数: " + totalInstances);
        
        Button btnFinish = findViewById(R.id.btn_finish);
        btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        
        // 模拟用户输入的EditText，配置变更时需要保存和恢复其状态
        TextView etInput = findViewById(R.id.et_user_input);
        etInput.setText(userInput);
        
        // 使用TextView显示当前配置信息
        TextView tvConfig = findViewById(R.id.tv_current_config);
        updateConfigInfo(tvConfig);
    }
    
    /**
     * 更新当前配置信息显示
     */
    private void updateConfigInfo(TextView tvConfig) {
        Configuration config = getResources().getConfiguration();
        String orientation = (config.orientation == Configuration.ORIENTATION_LANDSCAPE) ? "横屏" : "竖屏";
        int screenWidthDp = config.screenWidthDp;
        int screenHeightDp = config.screenHeightDp;
        float fontScale = config.fontScale;
        
        StringBuilder sb = new StringBuilder();
        sb.append("屏幕方向: ").append(orientation).append("\n");
        sb.append("屏幕宽度: ").append(screenWidthDp).append("dp\n");
        sb.append("屏幕高度: ").append(screenHeightDp).append("dp\n");
        sb.append("字体缩放: ").append(fontScale);
        
        tvConfig.setText(sb.toString());
    }
    
    /**
     * 记录生命周期事件
     */
    private void logLifecycleEvent(String event) {
        Log.d(TAG, event);
        lifecycleLog.append(event).append("\n");
    }
    
    /**
     * 更新生命周期信息显示
     */
    private void updateLifecycleInfo() {
        tvLifecycleInfo.setText(lifecycleLog.toString());
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        logLifecycleEvent("onStart()");
        updateLifecycleInfo();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        logLifecycleEvent("onResume()");
        updateLifecycleInfo();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        logLifecycleEvent("onPause()");
        // 保存用户输入
        TextView etInput = findViewById(R.id.et_user_input);
        userInput = etInput.getText().toString();
        updateLifecycleInfo();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        logLifecycleEvent("onStop()");
    }
    
    @Override
    protected void onRestart() {
        super.onRestart();
        logLifecycleEvent("onRestart()");
        updateLifecycleInfo();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        logLifecycleEvent("onDestroy()");
    }
    
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        // 保存当前日志和用户输入
        outState.putString("lifecycle_log", lifecycleLog.toString());
        outState.putString("user_input", userInput);
        logLifecycleEvent("onSaveInstanceState()");
        updateLifecycleInfo();
    }
    
    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        logLifecycleEvent("onRestoreInstanceState()");
        updateLifecycleInfo();
        
        // 显示恢复的用户输入
        TextView etInput = findViewById(R.id.et_user_input);
        etInput.setText(userInput);
    }
} 